package com.kvn.main;

import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleConfig;
import com.alipay.jarslink.api.ModuleLoader;
import com.alipay.jarslink.api.ModuleManager;
import com.alipay.jarslink.api.impl.ModuleManagerImpl;
import com.google.common.collect.ImmutableList;
import com.kvn.jarslink.ModuleScanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhiyuan.wang on 2018/3/29.
 */
@SpringBootApplication
@ImportResource({"classpath*:jarslink.xml"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }


    @Bean
    public ModuleManager moduleManager(ModuleLoader moduleLoader) throws IOException {
        // 扫描模块
        List<ModuleConfig> moduleConfigs = ModuleScanner.doScan();
        // 加载、注册模块
        ModuleManager moduleManager = new ModuleManagerImpl();
        for (ModuleConfig moduleConfig : moduleConfigs) {
            Module module = moduleLoader.load(moduleConfig);
            moduleManager.register(module);
        }

        return moduleManager;
    }
}
